package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.WalletFinancingOwn;
import com.chainyoung.component_tmperson.entity.WalletProfiRecord;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletFinancingProfitContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletFinancingTradeContract;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;
import static com.chainyoung.common.utils.RxUtils.rxSchedulerHelper;

public class WalletFinancingProfitPresenter extends BasePresenter<WalletFinancingProfitContract.Model, WalletFinancingProfitContract.View> {

    @Inject
    public WalletFinancingProfitPresenter(WalletFinancingProfitContract.Model model, WalletFinancingProfitContract.View rootView) {
        super(model, rootView);
    }


    public void getUserFinancingProfit(TreeMap<String, Object> map) {

        addSubscribe(mModel.getUserFinancingProfit(map)
                .compose(RxUtils.<WalletFinancingOwn>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<WalletFinancingOwn>(mView, false) {
                    @Override
                    public void onNext(WalletFinancingOwn data) {
                        mView.onGetUserFinancingProfitSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onGetUserFinancingProfitFailed();
                        super.onError(e);
                    }
                }));
    }


    public void turnOut(TreeMap<String, Object> map) {

        addSubscribe(mModel.turnOut(map)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView, true) {
                    @Override
                    public void onNext(String data) {
                        mView.onTurnOutSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


    public void getFinancingProfitList(TreeMap<String, Object> map) {

        addSubscribe(mModel.getFinancingProfitList(map)
                .compose(RxUtils.<CommonList<WalletProfiRecord>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonList<WalletProfiRecord>>(mView, false) {
                    @Override
                    public void onNext(CommonList<WalletProfiRecord> data) {
                        mView.onGetFinancingProfitListSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetFinancingProfitListSuccess(null);
                                mView.hideLoading();
                                return;
                            }
                        }
                        mView.onGetFinancingProfitListFailed();
                        super.onError(e);
                    }
                }));
    }

}





